﻿using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections.Generic;
using CoreLib.DAL;
using CoreLib.BusinessLogic;

namespace RKMSikraKulingram
{
    internal struct ModifiedData 
    {
        public string ColumnName;
        public object ModifiedColumnValue;

        public ModifiedData(string columnName, object columnValue)
        {
            this.ColumnName = columnName;
            this.ModifiedColumnValue = columnValue;
        }
    }

    public partial class PersonTransactionsForm : Form
    {
        # region Private Data Members

        private int _personID;
        private bool _personIsDirty;
        private Person _person;

        private int _addressID;
        private bool _addressIsDirty;
        private Address _address;

        private int _transactionID;
        private bool _transactionIsDirty;
        private Transaction _transaction;

        private int _transactionReceiptDetailID;
        private bool _transactionReceiptDetailIsDirty;
        private TransactionReceiptDetail _transactionReceiptDetail;

        private DevoteeDetails _devoteeDetails = null;

        private Dictionary<int, List<int>> _modifiedDataRowAndColumnNumberList = new Dictionary<int, List<int>>();
        Dictionary<int, List<ModifiedData>> _personModifiedDatae = new Dictionary<int, List<ModifiedData>>();
        Dictionary<int, List<ModifiedData>> _addressModifiedDatae = new Dictionary<int, List<ModifiedData>>();
        Dictionary<int, List<ModifiedData>> _transactionModifiedDatae = new Dictionary<int, List<ModifiedData>>();
        Dictionary<int, List<ModifiedData>> _transactionReceiptDetailsDatae = new Dictionary<int, List<ModifiedData>>();

        # endregion

        # region Constructors

        public PersonTransactionsForm()
        {
            InitializeComponent();
        }

        public PersonTransactionsForm(int personID) : this()
        {
            this._personID = personID;
            this.dateTimePickerEntryDateFrom.Value = this.dateTimePickerEntryDateFrom.MinDate;
            this.dateTimePickerEntryDateTo.Value = DateTime.Now;
        }

        public PersonTransactionsForm(int personID, DateTime entryDateFrom, DateTime entryDateTo)
            : this(personID)
        {
            this.dateTimePickerEntryDateFrom.Value = entryDateFrom;
            this.dateTimePickerEntryDateTo.Value = entryDateTo;
        }

        # endregion Constructors

        # region Event Handlers

        # region Form Specific Even Handlers

        private void PersonTransactionsForm_Load(object sender, EventArgs e)
        {
            this.Owner.Enabled = false;
            refreshSearch();
        }

        private void Search_Click(object sender, EventArgs e)
        {
            refreshSearch();
        }

        void PersonTransactionsForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            this.Owner.Enabled = true;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (_modifiedDataRowAndColumnNumberList.Count > 0)
            {
                foreach (KeyValuePair<int, List<int>> mod in _modifiedDataRowAndColumnNumberList)
                {
                    _personIsDirty = false;
                    _addressIsDirty = false;
                    _transactionIsDirty = false;
                    _transactionReceiptDetailIsDirty = false;

                    _personID = int.Parse(
                        this.dataGridViewPersonTransactionDetails.Rows[mod.Key].
                        Cells["PersonID"].Value.ToString());
                    _addressID = int.Parse(
                        this.dataGridViewPersonTransactionDetails.Rows[mod.Key].
                        Cells["AddressID"].Value.ToString());
                    _transactionID = int.Parse(
                        this.dataGridViewPersonTransactionDetails.Rows[mod.Key].
                        Cells["TransactionID"].Value.ToString());
                    _transactionReceiptDetailID = int.Parse(
                        this.dataGridViewPersonTransactionDetails.Rows[mod.Key].
                        Cells["TransactionReceiptDetailID"].Value.ToString());

                    _person = CommonBLL.CreateCommonBLL().GetPersonByPersonID(_personID);
                    _person.PropertyChanged += (o, e1) => { _personIsDirty = true; };
                    
                    _address = CommonBLL.CreateCommonBLL().GetAddressByAddressID(_addressID);
                    _address.PropertyChanged += (o, e1) => { _addressIsDirty = true; };

                    _transaction = CommonBLL.CreateCommonBLL().GetTransactionByTransactionID(_transactionID);
                    _transaction.PropertyChanged += (o, e1) => { _transactionIsDirty = true; };

                    _transactionReceiptDetail = CommonBLL.CreateCommonBLL().GetTransactionReceiptDetailByTransactionReceiptDetailID(_transactionReceiptDetailID);
                    _transactionReceiptDetail.PropertyChanged += (o, e1) => { _transactionReceiptDetailIsDirty = true; };
                }
            }
        }

        # endregion Form Specific Even Handlers

        # region DataGridView PersonTransactionDetails Specific Event Handlers

        void dataGridViewPersonTransactionDetails_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            if (this._modifiedDataRowAndColumnNumberList.ContainsKey(e.RowIndex))
                this._modifiedDataRowAndColumnNumberList[e.RowIndex].Add(e.ColumnIndex);
            else
            {
                List<int> columnIndexList = new List<int>();
                columnIndexList.Add(e.ColumnIndex);
                this._modifiedDataRowAndColumnNumberList.Add(e.RowIndex, columnIndexList);
            }
        }

        void dataGridViewPersonTransactionDetails_CellDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            _personID = int.Parse(this.dataGridViewPersonTransactionDetails.Rows[e.RowIndex].Cells["PersonID"].Value.ToString());
            _transactionID = int.Parse(this.dataGridViewPersonTransactionDetails.Rows[e.RowIndex].Cells["TransactionID"].Value.ToString());
            _addressID = int.Parse(this.dataGridViewPersonTransactionDetails.Rows[e.RowIndex].Cells["AddressID"].Value.ToString());
            _transactionReceiptDetailID = int.Parse(this.dataGridViewPersonTransactionDetails.Rows[e.RowIndex].Cells["TransactionReceiptDetailID"].Value.ToString());

            _devoteeDetails = new DevoteeDetails(_personID, _addressID, _transactionID, _transactionReceiptDetailID);
            _devoteeDetails.DevoteeDetailsClosing += (o, e1) =>
                {
                    refreshSearch();
                };
            _devoteeDetails.Show(this);
        }

        # endregion DataGridView PersonTransactionDetails Specific Event Handlers

        # endregion Event Handlers

        # region Private Methods
                
        private void refreshSearch()
        {
            this.searchTransactionsByPersonIDTableAdapter.Fill
                (this.sikraDataSet.SearchTransactionsByPersonID,
                _personID, this.dateTimePickerEntryDateFrom.Value, this.dateTimePickerEntryDateTo.Value);

            this.dataGridViewPersonTransactionDetails.Refresh();
        }

        # endregion Private Methods
    }
}
